package com.zz.spring6.tx.service;

import com.zz.spring6.tx.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

//@Transactional(isolation = Isolation.DEFAULT//使用数据库默认的隔离级别
// @Transactional(isolation= IsolatiOn.READ UNCOMMITTED)读未提交
// @Transactional(isolation= Isolation.READ_COMMITTED)//读已提交
// @Transactional(isolation = IsolatiOn.REPEATABLE_READ)//可重复读
// @Transactional(isolation= Isolation.SERIALIZABLE)//串行化

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Service
public class BookServiceImpl implements BookService{


    @Autowired
    private BookDao bookDao;


    @Override
    public void buyBook(Integer boosId, Integer userId) {

        //  模拟超时
//        try {
//            TimeUnit.SECONDS.sleep(5);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        Integer price =bookDao.getBookPriceByBookId(boosId);

        bookDao.updateStock(boosId);

        bookDao.updateUserBalance(userId,price);

    }
}
